package  
{
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	import org.flixel.FlxText;
	import org.flixel.FlxG;
	import org.flixel.FlxU;
	/**
	 * ...
	 * @author morgan
	 */
	public class EndingStage extends FlxState
	{
		[Embed (source = "../data/victory.png")] private var img_victory:Class;
		[Embed (source = "../data/ending/sun.png")] private var img_sun:Class;
		[Embed (source = "../data/ending/bg0.png")] private var img_lowbg:Class;
		[Embed (source = "../data/ending/bg1.png")] private var img_midbg:Class;
		[Embed (source = "../data/ending/bg2.png")] private var img_terrain:Class;
		[Embed (source = "../data/ending/bg2_2.png")] private var img_terrain2:Class;
		[Embed (source = "../data/ending/dog.png")] private var img_dog:Class;
		[Embed(source="../data/visitor1.ttf", fontFamily="visitor", embedAsCFF="false")] public var font_visitor:String;		
		
		public var scoreText:FlxText;
		public var sun:FlxSprite;
		public var lowbg:FlxSprite;
		public var midbg:FlxSprite;
		public var terrain:FlxSprite;
		public var titleTerrain:FlxSprite;
		public var bestText:FlxText;
		public var dog:FlxSprite;
		
		public var time:Number = -3;
		public var particleTimer:Number = 0;
		
		public function EndingStage() 
		{
			
		}
		
		override public function create():void 
		{
			super.create();
			FlxG.bgColor = 0xff7f90d2;
			
			
			lowbg = new FlxSprite(0, 240 - 194, img_lowbg); add(lowbg);
			midbg = new FlxSprite(0, 240 - 179, img_midbg); add(midbg);
			
			scoreText = new FlxText(0, 10, 320, "Score: 5324232", true); add(scoreText);
			scoreText.setFormat("visitor", 20, 0xff7f90d2, "center");
			scoreText.text = "score: " + Math.floor(FlxG.score).toString();			
			
			sun = new FlxSprite( 118, -20, img_sun); add(sun);
			terrain = new FlxSprite(0, 240 - 122, img_terrain); add(terrain);
			
			dog = new FlxSprite(320, 85, img_dog);
			add(dog);			
			
			titleTerrain = new FlxSprite(0, 240 - 122, img_terrain2); add(titleTerrain);
			titleTerrain.alpha = 0;
			
			bestText = new FlxText(0, 140, 320, "", true);
			bestText.setFormat("visitor", 30, 0xffffff, "center");
			add(bestText)
			
			midbg.color = FlxU.makeColor(127, 144, 210);
			lowbg.color = FlxU.makeColor(127, 144, 210);
			sun.color = FlxU.makeColor(255, 228, 0);		
			
			FlxG.flash(0xffffff, 2);
			
		}
		
		override public function update():void 
		{
			super.update();
			time += FlxG.elapsed
			
			var t_c:Number = FlxU.bound((time) / 7 + 0.5, -2, Math.PI / 2);
			sun.x = Math.sin(t_c) * -57 + 118;
			sun.y = Math.cos(t_c) * -100 + 80;			
			if (time < 0)
			{
				return;
			}			
			
			var t_a:Number = FlxU.bound((time - 4) / 6, 0, 1);
			var t_b:Number = FlxU.bound((time - 2) / 7, 0, 1);
			
			titleTerrain.alpha = t_b;
			// 127, 144, 210
			// 100, 57, 68
			// 51, 55, 69
			// 38, 42, 57
			// 255, 0, 66
			// 255, 228, 0
			// 118, -20 -> 61, 80
			midbg.color = FlxU.makeColor(127 - 27 * t_a, 144 - 87 * t_a, 210 - 142 * t_a, 1.0);
			lowbg.color = FlxU.makeColor(127 - (127 - 51) * t_a, 144 - (144 - 55) * t_a, 210 - (210 - 69) * t_a);
			FlxG.bgColor = FlxU.makeColor(127 - (127 - 38) * t_a, 144 - (144 - 42) * t_a, 210 - (210 - 57) * t_a);
			sun.color = FlxU.makeColor(255, 228 * (1 - t_b), 66 * t_b);
			
			dog.scale.x = dog.scale.y = 1.0 / (time * 2 + 1);
			dog.velocity.x = -70 / (time+0.25);
			dog.velocity.y = -50 / (time + 3) + time;
			
			particleTimer += FlxG.elapsed;
			
			if (particleTimer > 0.05 && time < 8)
			{
				particleTimer = 0;
				var p:Particle = new Particle();
				add(p);
				p.scale.y = (p.scale.x = dog.scale.x * 3) / 2;
				p.spawn(Particle.THRUSTER, dog.x + dog.width / 2 + (Math.random() - 0.5) * 10 * p.scale.x, dog.y + dog.height / 2 + 30 * p.scale.x);
				p.velocity.y = 20 * p.scale.x;
				//p.velocity.y = (Math.random() * 60 - 30) * p.scale.x;
				//p.velocity.x = (Math.random() * 60 - 30) * p.scale.x;
			}			
			
			if (time < 8.0 && time + FlxG.elapsed >= 8.0)
			{
				bestText.text = "Hunger Dog"
			}
			if (time < 9.5 && time + FlxG.elapsed >= 9.5)
			{
				bestText.text = "You did it";
			}
			if (time < 11 && time + FlxG.elapsed >= 11)
			{
				bestText.text = "You are the best.";
			}
			bestText.alpha = Math.sin(time * 4) * 0.25 + 0.75;
			if (time > 17.0)
			{
				FlxG.score = 0;
				FlxG.switchState(new TitleState());
			}
		}
		
	}

}